﻿using System;
using Medianamik.Core;
using Medianamik.Globalization;
using Telerik.Web.UI;

namespace Medianamik.UI.Controls
{
    public class DateTimePicker : BaseMedianamikControl
    {
        protected RadDateTimePicker radDatePicker = new RadDateTimePicker();

        protected override void CreateControlHierarchy(bool pUseDataSource)
        {

            Controls.Add(radDatePicker);
            radDatePicker.ID = ControlToValidateId;

            radDatePicker.MinDate = new DateTime(1901, 1, 1);
            //Cette traduction ne fonctionne pas bien avec les resources de Telerik, override
            radDatePicker.DatePopupButton.ToolTip = Translator.Translate("Medianamik_Controls", "OpenCalendarPopup");
            radDatePicker.Calendar.FastNavigationSettings.CancelButtonCaption = Translator.Translate("Medianamik_Controls", "CancelButton");
            radDatePicker.Calendar.FastNavigationSettings.TodayButtonCaption = Translator.Translate("Medianamik_Controls", "TodayButton");
            radDatePicker.Calendar.ShowRowHeaders = false;
            radDatePicker.Calendar.UseColumnHeadersAsSelectors = false;
            radDatePicker.Calendar.EnableMonthYearFastNavigation = false;
            radDatePicker.DateInput.CssClass = "TxtBox";
            radDatePicker.Culture = MedianamikContext.Current.CurrentUICulture;

            //radDatePicker.EnableEmbeddedSkins = false;
            //radDatePicker.Skin = "Medianamik";

            if (pUseDataSource)
            {
                var date = (DateTime?)DataSource;
                if (date.HasValue)
                {
                    radDatePicker.SelectedDate = date.Value;
                }
                radDatePicker.CssClass = "DatePicker";

                radDatePicker.Width = ControlInfo.Width.HasValue ? ControlInfo.Width.Value : WIDTH;
            }

        }

        protected override bool WrapContentInParagraph
        {
            get { return false; }
        }

        public override Type TypeOfValue
        {
            get { return typeof(DateTime?); }
        }

        public override object GetValue()
        {
            EnsureChildControls();
            return radDatePicker.SelectedDate;
        }

        public override void SetValue(object value)
        {
            EnsureChildControls();

            var date = value as DateTime?;

            if (date.HasValue)
                radDatePicker.SelectedDate = date.Value;
            else
                radDatePicker.Clear();
        }
    }
}